// pages/payInfo/payInfo.js
const app = getApp()
var host = app.globalData.host;
var utils = app.globalData.utils;
var self = null;

Page({
  data: {
    themeColor: '',
    params: {},
    shopInfo: null,
    showAuth: false
  },
  onLoad: function (options) {
    self = this;
    self.getStyle();
    console.log(options);
    if (!options.q || decodeURIComponent(options.q).indexOf('?') < 0) {
      wx.showModal({
        title: '提示',
        content: '二维码无效',
        confirmText: '返回首页',
        showCancel: false,
        success: obj => {
          wx.reLaunch({
            url: '/pages/index/index'
          })
        }
      })
      return;
    }
    wx.getSetting({
      success: res => {
        if (!res.authSetting['scope.userInfo']) {
          wx.setStorageSync("path", self.route + '?q=' + options.q);
          self.setData({
            showAuth: true
          })
        }
      }
    })
    var q = decodeURIComponent(options.q);
    var paramStr = q.split('?')[1];
    var paramArray = paramStr.split('&');
    var params = {};
    for (var i = 0; i < paramArray.length; i ++) {
      var param = paramArray[i].split('=');
      params[param[0]] = param[1] ? param[1] : '';
    }
    console.log(q);
    console.log(params);
    if (params.total) {
      params.price = utils.accDiv(params.total, 100);
    }
    self.setData({
      params: params
    })
    if (params.shop) {
      self.getShopName();
    }
  },
  getStyle() {
    self.setData({
      themeColor: app.globalData.themeColor
    })
  },
  getShopName() {
    var url = host + '/yq/api/shopInfo';
    var params = {
      code: self.data.params.shop
    }
    var success = res => {
      console.log(res);
      self.setData({
        shopInfo: res.info
      })
    }
    var fail = res => { }
    var complete = () => { }
    app.requestLoading(url, params, success, fail, complete);
  },
  goPay() {
    var url = host + '/yq/api/directPay';
    var params = {
      body: self.data.params.body,
      detail: self.data.params.detail,
      total: self.data.params.total,
      shop: self.data.params.shop,
      goodsTag: self.data.params.goodsTag,
      notifyUrl: self.data.params.notifyUrl
    }
    var success = res => {
      var status = res.status
      if (status == 204) {
        wx.showModal({
          title: '出错了',
          content: res.msg
        })
        return;
      }
      var response = res.info
      var nonce_str = response.nonce_str
      var prepay_id = response.prepay_id
      var sign = response.sign
      var timestamp = response.timeStamp
      var appId = response.appid
      var param = {
        'appId': appId,
        'timeStamp': '' + timestamp,
        'nonceStr': '' + nonce_str,
        'package': 'prepay_id=' + prepay_id,
        'signType': 'MD5',
        'paySign': '' + sign,
        'success': function (res) {
          setTimeout(function () {
            wx.navigateTo({
              url: '/pages/success/success?isShowContent=' + 1
            })
          }, 200)

        },
        'fail': function (res) {
          wx.showModal({
            title: '出错了',
            content: '支付失败'
          })
        }
      }
      wx.requestPayment(param);
      // wx.showModal({
      //   title: '提示',
      //   content: '支付成功',
      //   confirmText: '返回首页',
      //   showCancel: false,
      //   success: obj => {
      //     wx.reLaunch({
      //       url: '/pages/index/index'
      //     })
      //   }
      // })
    }
    var fail = res => { }
    var complete = () => { }
    app.requestLoading(url, params, success, fail, complete);
  }
})